package selcoursesys.dao.impl;

import db.MyHibernateSessionFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import selcoursesys.dao.SelectionDAO;
import selcoursesys.entity.SelectionEntity;

import javax.print.DocFlavor;
import java.util.Iterator;
import java.util.List;

/**
 * Created by Lpbdz on 2017/4/9.
 */
public class SelectionDAOImpl extends BaseHibernateDAOImpl implements SelectionDAO{
    private Session session;
    public boolean delSelection(SelectionEntity selectionEntity){
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            String hql="delete SelectionEntity selection where selection.stuid=:stuid and selection.courseid=:courseid";
            Query query=session.createQuery(hql);
            query.setString("stuid",selectionEntity.getStuid());
            query.setString("courseid",selectionEntity.getCourseid());
            query.executeUpdate();
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            MyHibernateSessionFactory.closeSession();
            return true;
        }
    }

    public boolean checkSelection(SelectionEntity selectionEntity){
        session= MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From SelectionEntity student where student.stuid=:stuid ";
        Query query=session.createQuery(hql);
        query.setString("stuid",selectionEntity.getStuid());
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        if(list!=null&&list.size()>0){
            return true;
        }
        return false;
    }

    public boolean insertSelection(SelectionEntity selectionEntity){
        /**
        StudentEntity studentEntity=new StudentEntity();
        studentEntity.setStuid(selectionEntity.getStuid());
        StudentDAO studentDAO=new StudentDAOImpl();
        StudentEntity studentEntity1=studentDAO.checkStudent(studentEntity);
        System.out.println(studentEntity1.getStuname());
        studentEntity1.getCourse().add(courseEntity);
        */
        Transaction tx=null;
        try{
            session=MyHibernateSessionFactory.getSession();
            tx=session.beginTransaction();
            session.save(selectionEntity);
            tx.commit();
        }catch (Exception e){
            e.printStackTrace();
            tx.rollback();
            return false;
        }finally {
            MyHibernateSessionFactory.closeSession();
            return true;
        }
    }

    public List querySelection(SelectionEntity selectionEntity){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        //String hql="select s.stuid,c.courseid from StudentEntity s inner join s.course c where s.stuid=:stuid";
        String hql="FROM SelectionEntity selection where selection.stuid=:stuid";
        Query query=session.createQuery(hql);
        query.setString("stuid",selectionEntity.getStuid());

        List   list=query.list();
        Iterator iterator=list.iterator();
        MyHibernateSessionFactory.closeSession();
        return list;
    }
    //检查课程是否存在于选课表
    public boolean checkSelectionExit(String courseid,String stuid){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="FROM SelectionEntity selection where selection.stuid=:stuid and selection.courseid=:courseid";
        Query query=session.createQuery(hql);
        query.setString("stuid",stuid);
        query.setString("courseid",courseid);
        List list=query.list();
        MyHibernateSessionFactory.closeSession();
        if(list.size()>0){
            return true;
        }
        return false;
    }
    //选择课程通过课程id,用于判断数量冲突
    public List<SelectionEntity> querySelectCid(String courseid){
        session=MyHibernateSessionFactory.getSession();
        session.clear();
        String hql="From SelectionEntity selection where selection.courseid=:courseid";
        Query query=session.createQuery(hql);
        query.setString("courseid",courseid);
        List list=null;
        list=query.list();
        MyHibernateSessionFactory.closeSession();
        return list;
    }
}
